package com.decheng.filter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author 孔德成
 * @Slogan 致敬大师，致敬未来的你
 * @desc 登陆过滤器：购物车请求拦截
 */
public class LoginFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        // 1. 查看session中是否有用户登陆信息
        Object loginUser = request.getSession().getAttribute("loginUser");
        if (loginUser != null) {
            // 2. 用户已登陆，放行
            chain.doFilter(request, response);
        } else {
            // 3. 未登陆，重定向到登陆页面
            response.sendRedirect(request.getContextPath() + "/user?method=toLoginPage");
        }

    }
}
